from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from rest_framework.generics import GenericAPIView

from UserMan.models import UserPeo
from UserMan.pac import start


class Index(GenericAPIView):
    def get(self, request):
        return render(request, "index.html")


class Login(GenericAPIView):
    def get(self, request):
        return render(request, "login.html")

    def post(self, request):
        username = request.POST.get("username")
        if not UserPeo.objects.filter(u_name=username).exists():
            return render(request, "none.html")
        user = UserPeo.objects.get(u_name=username)
        password = request.POST.get("password")
        pik = user.verity_password(password)

        return render(request, "center.html", context=locals())


class Register(GenericAPIView):
    def get(self, request):
        return render(request, "register.html")

    def post(self, request):
        user = UserPeo()
        user.u_name = request.POST.get("username")
        user.u_password = request.POST["password"]
        user.save()

        return render(request, "login.html")


class Logout(GenericAPIView):
    def get(self, request):
        return render(request, "logout.html")


class Crawler(GenericAPIView):
    def get(self, request):
        return HttpResponse("<h1>请从登陆页面转跳</h1>")

    def post(self, request):
        pics = request.POST.get("pics")
        nums = request.POST.get("nums")
        nums = int(nums)
        d = start(pics, nums)

        return render(request, "crawlers.html", context=locals())